﻿using System;
using System.Web;
using System.Web.Mvc;

using Hasiu.Entity;
using Hasiu.Service;
using Hasiu.Mvc.Filters;
using Hasiu.Mvc.Infrastructure;
using Hasiu.Mvc.ViewModels;

namespace Hasiu.Mvc.Controllers.Admin
{
    [AdminAuthorize]
    public class SlideAdminController : Controller
    {
        public ActionResult Index(string id)
        {
            var slideViewModel = new SlideViewModel
                                     {
                                         GetSlideList = SlideService._.GetList()
                                     };
            if (!string.IsNullOrEmpty(id))
            {
                slideViewModel.GetSlide = SlideService._.Get(id);
            }
            return View(slideViewModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(Slide slide)
        {
            if (string.IsNullOrEmpty(slide.Id))
            {
                slide.Id = Guid.NewGuid().ToString("N");
                slide.Title = slide.Title ?? "";
                slide.Url = slide.Url ?? "";
                slide.Img = slide.Img.MoveFile("/file/slide/");
                SlideService._.Save(slide);
                return new RedirectOfAddResult("/admin/slide.aspx");
            }
            var slide2 = SlideService._.Get(slide.Id);
            if (slide2.Img != slide.Img)
            {
                slide2.Img.DeleteFile();
                slide2.Img = slide.Img.MoveFile("/file/slide/");
            }
            slide2.Title = slide.Title;
            slide2.Url = slide.Url;
            SlideService._.Update(slide2);
            return new RedirectOfUpdateResult("/admin/slide.aspx");
        }

        public ActionResult Del(string id)
        {
            SlideService._.Delete(id);
            return new RedirectOfDeleteResult("/admin/slide.aspx");
        }
    }
}
